<!DOCTYPE html>
<html lang="zh-hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/wow/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/wow/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/wow/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/wow/images/logo.svg" color="#222">

<link rel="stylesheet" href="/wow/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"single-flower.gitee.io","root":"/wow/","images":"/wow/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"بحث...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="1、注册一个沙箱号：https:&#x2F;&#x2F;open.alipay.com&#x2F;platform&#x2F;home.htm 一、安装插件1pip install python-alipay-sdk --upgrade 二、在settings中配置123456ALIPAY_APPID &#x3D; &amp;#x27;&amp;#x27;ALIPAY_URL &#x3D; &quot;&quot;ALIPAY_DEBUG &#x3D; TrueAPP_PRIVAT">
<meta property="og:type" content="article">
<meta property="og:title" content="三方支付">
<meta property="og:url" content="https://single-flower.gitee.io/wow/2021/02/01/zy/%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/index.html">
<meta property="og:site_name" content="朵朵最可爱">
<meta property="og:description" content="1、注册一个沙箱号：https:&#x2F;&#x2F;open.alipay.com&#x2F;platform&#x2F;home.htm 一、安装插件1pip install python-alipay-sdk --upgrade 二、在settings中配置123456ALIPAY_APPID &#x3D; &amp;#x27;&amp;#x27;ALIPAY_URL &#x3D; &quot;&quot;ALIPAY_DEBUG &#x3D; TrueAPP_PRIVAT">
<meta property="og:locale">
<meta property="article:published_time" content="2021-02-01T04:04:56.571Z">
<meta property="article:modified_time" content="2021-04-13T07:05:19.974Z">
<meta property="article:author" content="X y">
<meta property="article:tag" content="-GIT">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://single-flower.gitee.io/wow/2021/02/01/zy/%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-hans'
  };
</script>
<title>三方支付 | 朵朵最可爱</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="تشغيل شريط التصفح" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/wow/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">朵朵最可爱</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/wow/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-about"><a href="/wow/about/" rel="section"><i class="fa fa-user fa-fw"></i>معلومات</a></li>
        <li class="menu-item menu-item-tags"><a href="/wow/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>الوسوم</a></li>
        <li class="menu-item menu-item-categories"><a href="/wow/categories/" rel="section"><i class="fa fa-th fa-fw"></i>التصنيفات</a></li>
        <li class="menu-item menu-item-archives"><a href="/wow/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>الأرشيفات</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          المحتويات
        </li>
        <li class="sidebar-nav-overview">
          عام
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%AE%89%E8%A3%85%E6%8F%92%E4%BB%B6"><span class="nav-number">1.</span> <span class="nav-text">一、安装插件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%9C%A8settings%E4%B8%AD%E9%85%8D%E7%BD%AE"><span class="nav-number">2.</span> <span class="nav-text">二、在settings中配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%8F%91%E8%B5%B7%E6%94%AF%E4%BB%98"><span class="nav-number">3.</span> <span class="nav-text">三、发起支付</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E4%BF%9D%E5%AD%98%E6%94%AF%E4%BB%98%E7%BB%93%E6%9E%9C"><span class="nav-number">4.</span> <span class="nav-text">五、保存支付结果</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">5.</span> <span class="nav-text">总结</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">X y</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/wow/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">المقالات</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">التصنيفات</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">الوسوم</span>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-hans">
    <link itemprop="mainEntityOfPage" href="https://single-flower.gitee.io/wow/2021/02/01/zy/%E4%B8%89%E6%96%B9%E6%94%AF%E4%BB%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wow/images/avatar.gif">
      <meta itemprop="name" content="X y">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="朵朵最可爱">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          三方支付
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">نُشر في</span>

      <time title="أُنشأ: 2021-02-01 12:04:56" itemprop="dateCreated datePublished" datetime="2021-02-01T12:04:56+08:00">2021-02-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">عُدل في</span>
        <time title="عُدل: 2021-04-13 15:05:19" itemprop="dateModified" datetime="2021-04-13T15:05:19+08:00">2021-04-13</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">في</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/wow/categories/GIT/" itemprop="url" rel="index"><span itemprop="name">-GIT</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>1、注册一个沙箱号：<a target="_blank" rel="noopener" href="https://open.alipay.com/platform/home.htm">https://open.alipay.com/platform/home.htm</a></p>
<h2 id="一、安装插件"><a href="#一、安装插件" class="headerlink" title="一、安装插件"></a>一、安装插件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install python-alipay-sdk --upgrade</span><br></pre></td></tr></table></figure>
<h2 id="二、在settings中配置"><a href="#二、在settings中配置" class="headerlink" title="二、在settings中配置"></a>二、在settings中配置</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ALIPAY_APPID = <span class="string">&#x27;&#x27;</span></span><br><span class="line">ALIPAY_URL = <span class="string">&quot;&quot;</span></span><br><span class="line">ALIPAY_DEBUG = True</span><br><span class="line">APP_PRIVATE_KEY_PATH = <span class="string">&#x27;&#x27;</span></span><br><span class="line">ALIPAY_PUBLIC_KEY_PATH = <span class="string">&#x27;&#x27;</span></span><br><span class="line">ALIPAY_GATE = <span class="string">&#x27;https://openapi.alipaydev.com/gateway.do?&#x27;</span></span><br></pre></td></tr></table></figure>
<h2 id="三、发起支付"><a href="#三、发起支付" class="headerlink" title="三、发起支付"></a>三、发起支付</h2><p>请求方式：Get</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> rest_framework.views <span class="keyword">import</span> APIView</span><br><span class="line"><span class="keyword">from</span> rest_framework.response <span class="keyword">import</span> Response</span><br><span class="line"><span class="keyword">from</span> orders.models <span class="keyword">import</span> *</span><br><span class="line"><span class="keyword">from</span> rest_framework <span class="keyword">import</span> status</span><br><span class="line">from alipay import AliPay  #支付</span><br><span class="line"><span class="keyword">from</span> mall <span class="keyword">import</span> settings</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class AliPayURLView(APIView):</span><br><span class="line">    def get(self, request, order_id):</span><br><span class="line">        # 根据order_id查询订单对象</span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            order_obj = OrderInfo.objects.get(pk=order_id)</span><br><span class="line">        except:</span><br><span class="line">            raise Exception(<span class="string">&quot;订单号无效&quot;</span>)</span><br><span class="line"></span><br><span class="line">        # 创建alipay对象</span><br><span class="line">        alipay = AliPay(</span><br><span class="line">            appid=settings.ALIPAY_APPID,</span><br><span class="line">            app_notify_url=None,</span><br><span class="line">            app_private_key_path=settings.ALIPAY_PRIVATE_KEY_PATH,</span><br><span class="line">            alipay_public_key_path=settings.ALIPAY_PUBLIC_KEY_PATH,</span><br><span class="line">            debug=settings.ALIPAY_DEBUG</span><br><span class="line">        )</span><br><span class="line">    subject=<span class="string">&#x27;支付宝支付&#x27;</span></span><br><span class="line">        # 跳转到 https://openapi.alipay.com/gateway.do? + order_string</span><br><span class="line">    order_string = alipay.api_alipay_trade_page_pay(</span><br><span class="line">        out_trade_no=order_id,</span><br><span class="line">        total_amount=str(order.total_amount), # 这里类型我们要由 decimal转换为 str</span><br><span class="line">        subject=subject,</span><br><span class="line">        return_url=<span class="string">&quot;http://127.0.0.1/pay_success.html&quot;</span></span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    #  拼接url,并且返回</span><br><span class="line">    url = settings.ALIPAY_URL + <span class="string">&#x27;?&#x27;</span> + order_string</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> Response(&#123;<span class="string">&#x27;alipay_url&#x27;</span>:url&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="五、保存支付结果"><a href="#五、保存支付结果" class="headerlink" title="五、保存支付结果"></a>五、保存支付结果</h2><p>请求方式： PUT<br>路径：/pay/?参数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">class PayStatusAPIView(APIView):</span><br><span class="line">   def put(self,request):</span><br><span class="line">   # 1. 创建alipay对象</span><br><span class="line">   app_private_key_string = settings.APP_PRIVATE_KEY_PATH</span><br><span class="line">   alipay_public_key_string = settings.ALIPAY_PUBLIC_KEY_PATH</span><br><span class="line"></span><br><span class="line">   alipay = AliPay(</span><br><span class="line">       appid=settings.ALIPAY_APPID,</span><br><span class="line">       app_notify_url=None,  # 回调url</span><br><span class="line">       app_private_key_string=app_private_key_string,</span><br><span class="line">       # 支付宝的公钥，验证支付宝回传消息使用</span><br><span class="line">       alipay_public_key_string=alipay_public_key_string,</span><br><span class="line">       sign_type=<span class="string">&quot;RSA2&quot;</span>,  </span><br><span class="line">       debug=settings.ALIPAY_DEBUG  # 默认False</span><br><span class="line">   )</span><br><span class="line"></span><br><span class="line">   # 2.验证数据</span><br><span class="line">   data = request.query_params.dict()</span><br><span class="line">   # sign 不能参与签名验证</span><br><span class="line">   signature = data.pop(<span class="string">&quot;sign&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">   # verify确认是否成功</span><br><span class="line">   success = alipay.verify(data, signature)</span><br><span class="line">   <span class="keyword">if</span> success:</span><br><span class="line">       # 3.验证成功之后,可以从 data中获取 支付宝的订单id和 我们的订单id</span><br><span class="line">       # 支付宝的交易id</span><br><span class="line">       trade_id = data.get(<span class="string">&#x27;trade_id&#x27;</span>)</span><br><span class="line">       # 商家id</span><br><span class="line">       out_trade_id = data.get(<span class="string">&#x27;out_trade_id&#x27;</span>)</span><br><span class="line">       #1. 把支付宝的订单id和 我们的订单id 保存起来</span><br><span class="line">       Payment.objects.create(</span><br><span class="line">          order_id=out_trade_id,</span><br><span class="line">           trade_id=trade_id</span><br><span class="line">       )</span><br><span class="line">       # 2. 更新订单的状态</span><br><span class="line">       OrderInfo.objects.filter(order_id=out_trade_no).update(status=OrderInfo.ORDER_STATUS_ENUM[<span class="string">&#x27;UNSEND&#x27;</span>])</span><br><span class="line">       #3. 返回 支付宝的订单id</span><br><span class="line">       <span class="keyword">return</span> Response(&#123;<span class="string">&#x27;trade_id&#x27;</span>:trade_id&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>先创建沙箱账号，支付宝登录获取ALIPAY_APPID等信息，<br>保存在settings.py文件下然后进行视图的编写，<br>判断是否有下单时的订单号然后进行获取回调地址等信息，<br>最后回调返回支付成功保存数据完成支付接口</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/wow/tags/GIT/" rel="tag"># -GIT</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/wow/2021/01/30/zy/%E5%BE%AE%E5%8D%9A%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95/" rel="prev" title="微博三方登录">
                  <i class="fa fa-chevron-left"></i> 微博三方登录
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/wow/2021/02/03/zy/celery%E5%BC%82%E6%AD%A5%E6%A1%86%E6%9E%B6(5.0)/" rel="next" title="celery异步框架">
                  celery异步框架 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">X y</span>
</div>
  <div class="powered-by">تطبيق الموقع <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/wow/js/utils.js"></script><script src="/wow/js/motion.js"></script><script src="/wow/js/schemes/muse.js"></script><script src="/wow/js/next-boot.js"></script>

  






  





</body>
</html>
